package com.wj.controller;


import com.wj.config.MD5Util;
import com.wj.entity.UserEntity;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * @author WangJin
 * @version 1.0
 * @date 2022/2/12 16:01
 */
@Controller
public class LoginController {

    @RequestMapping("/login")
    public String login(){

        return "login";
    }

    /**
     * 验证码账号密码登录是否正确
     *
     * @return
     */
    @RequestMapping("/userLogin")
    public String userLogin(UserEntity user, HttpServletRequest request) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken =
                new UsernamePasswordToken(user.getUsername(), MD5Util.encode(user.getPassword()));
        try {
            subject.login(usernamePasswordToken);
            request.setAttribute("userName", user.getUsername());
            return "/home";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMsg", "账号或者密码错误");
            return "/index";
        }

    }

}
